<template>
  <div>
    <el-card class="box-card" style="margin:30px">
      <div slot="header" class="clearfix">
        <el-button size="small" type="primary" @click="add">新增</el-button>
      </div>
      <el-table
        :data="addressList"
        border
        style="width: 100%"
      >
        <el-table-column
          label="#"
          type="index"
          width="50"
          align="center"
        />
        <el-table-column
          prop="addressName"
          label="地址名称"
          width="200"
          align="center"
        />
        <el-table-column
          label="默认发货地址"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <span style="font-size:12px">默认发货地址:</span>
            <el-switch
              v-model="scope.row.sendStatus"
              :active-value="1"
              :inactive-value="0"
              @change="changeSend(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="收货人姓名"
          width="150"
          align="center"
        />
        <el-table-column
          prop="phone"
          label="收货人电话"
          width="150"
          align="center"
        />
        <el-table-column
          label="收货人地址"
          width="120"
          align="center"
        >
          <template slot-scope="scope">
            <div>{{ scope.row.province }}</div>
            <div>{{ scope.row.city }}</div>
            <div>{{ scope.row.detailAddress }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="默认收货地址"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <span style="font-size:12px">默认收货地址:</span>
            <el-switch
              v-model="scope.row.receiveStatus"
              :active-value="1"
              :inactive-value="0"
              @change="changeReceive(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="150"
          align="center"
        />
        <el-table-column
          fixed="right"
          label="操作"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" style="color:#f00" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <detail ref="details" />
  </div>
</template>
<script>
import { addressList, setReceiveOne, setSendOne, companyAddress } from '@/api/order/address'
import detail from './components/detail'
export default {
  components: {
    detail
  },
  data() {
    return {
      addressList: []
    }
  },
  computed: {
  },
  created() {
    this.getList()
  },
  methods: {
    changeSend(val) {
      var current = val.sendStatus === 0 ? 1 : 0
      setSendOne({ id: val.id }).then(res => {
        if (res.success) {
          this.$message.success('修改成功')
          this.getList()
        } else {
          this.$message.error('修改失败')
          val.sendStatus = current
        }
      })
    },
    changeReceive(val) {
      var current = val.receiveStatus === 0 ? 1 : 0
      setReceiveOne({ id: val.id }).then(res => {
        if (res.success) {
          this.$message.success('修改成功')
          this.getList()
        } else {
          this.$message.error('修改失败')
          val.receiveStatus = current
        }
      })
    },
    getList() {
      addressList().then(res => {
        console.log(res)
        this.addressList = res.data.items
      })
    },
    add() {
      this.$refs.details.open()
    },
    del(val) {
      companyAddress(val.id).then(res => {
        if (res.success) {
          this.$message.success('删除成功')
          this.getList()
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    edit(val) {
      this.$refs.details.open(val.id)
    }

  }
}
</script>
<style lang="scss" scoped>

</style>
